//
//  LoginViewController.m
//  LoverGo
//
//  Created by YeXiao on 14-1-18.
//  Copyright (c) 2014年 yexiao. All rights reserved.
//

#import "LGUtil.h"
#import "DataCenter.h"
#import "Toast+UIView.h"
#import "QuitRViewController.h"
#import "LoginViewController.h"
#import "FindPwdBackViewController.h"

@interface LoginViewController ()

@end

@implementation LoginViewController

-(id)init
{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSucc:) name:loginSPath object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSucc:) name:registerSPath object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSucc:) name:ErrorNotification object:nil];
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}
-(void)viewDidUnload
{
    [[NSNotificationCenter defaultCenter] removeObserver:loginSPath name:nil object:self];
    [[NSNotificationCenter defaultCenter] removeObserver:registerSPath name:nil object:self];
    [[NSNotificationCenter defaultCenter] removeObserver:ErrorNotification name:nil object:self];
}
-(void)loginSucc:(NSNotification *)nof
{
    if ([[nof name] compare:loginSPath] == 0) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    if ([[nof name] compare:ErrorNotification] == 0 ) {
        [[[[UIApplication sharedApplication] delegate] window] makeToast:@"登陆失败"
                                                                duration:1.0
                                                                position:@"center"];
    }
}
-(void)loadView
{
    
    [self dinglan];
    
    _loginView = [[LoginView alloc] initWithFrame:[LGUtil getViewFrame]];
    _loginView.delegate = self;
    
    [self setView:_loginView];
}
-(void)dinglan{
    UIButton * backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,20, 20)];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(dinglanBackBtn) forControlEvents:UIControlEventTouchUpInside] ;
    [backBtn setShowsTouchWhenHighlighted:YES] ;
    UIBarButtonItem *backbutton = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backbutton;
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(90, 0, 140, 30)];
    title.textAlignment = UITextAlignmentCenter;
    title.textColor = LGCOLOR_blue;
    title.text = @"用户登录";
    self.navigationItem.titleView = title;
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"barBG.png"] forBarMetrics:UIBarMetricsDefault];
}
-(void)dinglanBackBtn{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)LoginView:(LoginView *)view withType:(LoginType)type
{
    if (type == LoginTypeSub) {//登陆
        
        NSUserDefaults *uDef = [NSUserDefaults standardUserDefaults];
        [uDef setObject:[NSString stringWithFormat:@"%@",[view.nameTF text]] forKey:@"UserEmail"];
        [uDef setObject:[NSString stringWithFormat:@"%@",[view.passwordTF text]] forKey:@"UserPwd"];
        
        NSDictionary *para = [[NSDictionary alloc] initWithObjectsAndKeys:
                              [view.nameTF text],@"mobile",
                              [view.passwordTF text],@"passWord",
                              nil];
        [[RequestManager getSingleton] sendRequestAboutUser:loginSPath para:para];
    }
    if (type == LoginTypeBack) {
        [self.navigationController popViewControllerAnimated:YES];
    }else if (type == LoginTypeRegister)
    {
        QuitRViewController* vc = [[QuitRViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (type == TypeFindPwd) {//找回密码
        self.navigationItem.backBarButtonItem = [LGUtil getBackButton];
        FindPwdBackViewController* vc = [[FindPwdBackViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"登陆"];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"登陆"];
}
@end
